<script setup lang="ts">
import {markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import * as echarts from "echarts";
import {useGetPieDataApi} from "@/api";
import type {Pie,Common} from "@/interface";
import 'echarts-wordcloud'

const pie = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const pieData = ref<Pie>({
  data:[]
})
const init = async () => {
  const {data:res} = await useGetPieDataApi()
  pieData.value = res
  myChart.value = markRaw(echarts.init(pie.value));
  draw();
}
const draw = () => {
  option.value = {
    title: {
      text: '所有榜单小说的类型占比',
      left: 'center'
    },
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    legend: {
      type: 'scroll',
      orient: 'vertical',
      right: 10,
      top: 20,
      bottom: 20,
      data: pieData.value.data.map((item:Common)=>item.name)
    },
    series: [
      {
        name: '类别',
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 40,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: pieData.value.data,
        minAngle:3
      }
    ]
  }
  myChart.value.setOption(option.value, {notMerge: true});
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="pie" class="pie">
  </div>
</template>

<style scoped>
.pie{
  width: 100%;
  height: 100%;
}
</style>